import { defineStore } from 'pinia'
import { menu, permission } from "../api/module/user";

export const useUserStore = defineStore({
  id: 'user',
  state: () => {
    return {
      token: '',
      userInfo: {},
      permissions: [],
      menus: [],
    }
  },
  actions: {
    async loadMenus(){
      try {
        const response = await menu();
        this.menus = response.data;
      } catch (error) {
        console.error('加载菜单失败:', error);
      }
    },
    async loadPermissions(){
      try {
        const response = await permission();
        this.permissions = response.data;
      } catch (error) {
        console.error('加载权限失败:', error);
      }
    }
  },
  persist: {
    storage: localStorage,
    paths: ['token', 'userInfo', 'permissions', 'menus' ],
  }
})